package com.qianjiang.module.lib_common.base;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qianjiang.module.common.R;
import com.qianjiang.module.common.http.NetWorkUtils;

public abstract class BaseNewActivity extends AppCompatActivity {

    private LinearLayout llt_font_left;
    private ImageView iv_font_left;
    private TextView tv_font_title;
    private TextView tv_font_right;
    private LinearLayout llt_font_right;
    private ImageView iv_bar_right;
    private RelativeLayout rl_bar_base_layou;

    private FrameLayout mContentLayout;
    private TextView tv_bar_base_status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (initBundle(getIntent().getExtras())) {
            initActionView();
            initConfig();
            initBarView();
            initView();
            init();
            hasNet();
            initUrl();
            initData();
        } else {
            finish();
        }
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    private void initConfig(){
        Glide.with(this);// 绑定Context
    }

    protected boolean initBundle(Bundle bundle) {
        return true;
    }

    private void initActionView() {
        super.setContentView(R.layout.activity_base_new);
        rl_bar_base_layou = (RelativeLayout)findViewById(R.id.rl_bar_base_layou);
        tv_font_title = (TextView) findViewById(R.id.tv_font_title);
        mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
        llt_font_left = (LinearLayout)findViewById(R.id.llt_font_left);
        iv_font_left = (ImageView) findViewById(R.id.iv_font_left);
        llt_font_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_font_right = (TextView)findViewById(R.id.tv_font_right);
        llt_font_right = (LinearLayout)findViewById(R.id.llt_font_right);
        iv_bar_right = (ImageView)findViewById(R.id.iv_bar_right);

        tv_bar_base_status = (TextView) findViewById(R.id.tv_bar_base_status);
    }

    protected abstract void initBarView();

    protected void initView(){};


    protected void init() {
    }

    protected void initUrl() {
    }

    protected void initData() {
    }

    //设置ActionBar右侧的图片以及其点击事件
    protected ImageView setRightImage(int id) {
        iv_bar_right.setImageDrawable(ContextCompat.getDrawable(BaseNewActivity.this, id));
        llt_font_right.setVisibility(View.VISIBLE);
        return iv_bar_right;
    }

    protected ImageView setLeftImage(int id) {
        iv_font_left.setImageDrawable(ContextCompat.getDrawable(BaseNewActivity.this, id));
        llt_font_left.setVisibility(View.VISIBLE);
        return iv_font_left;
    }

    public void setBarBackground(String color){
        rl_bar_base_layou.setBackgroundColor(Color.parseColor(color));
    }

    public void setTitleBarGone(boolean isGone){
        if(isGone){
            llt_font_right.setVisibility(View.GONE);
        }else{
            llt_font_right.setVisibility(View.VISIBLE);
        }
    }

    public void setBackTitle(String backTitle){
        tv_font_title.setText(backTitle);
    }
    public void setBackGone(){
        tv_font_title.setVisibility(View.GONE);
        llt_font_left.setVisibility(View.GONE);
    }

    protected TextView setRightText(String desc) {
        tv_font_right.setText(desc);
        tv_font_right.setVisibility(View.VISIBLE);
        return tv_font_right;
    }

    protected TextView setRightTextColor(String color) {
        tv_font_right.setTextColor(Color.parseColor(color));
        return tv_font_right;
    }

    public void hasNet() {
        int netStatus = NetWorkUtils.getAPNType(BaseNewActivity.this);
        if (0 == netStatus) {
            tv_bar_base_status.setVisibility(View.VISIBLE);
        } else {
            tv_bar_base_status.setVisibility(View.GONE);
        }
    }


    public void setBarTitle(int titleId) {
        tv_font_title.setText(titleId);
    }

    public void setBarTitle(int titleId,boolean isShowBack) {
        tv_font_title.setText(titleId);
        if(isShowBack){
            tv_font_title.setVisibility(View.VISIBLE);
        }else{
            tv_font_title.setVisibility(View.GONE);
        }
    }

    public void setBarTitle(CharSequence title) {
        tv_font_title.setText(title);
    }

    public void setBarTitleColor(int stringId) {
        tv_font_title.setTextColor(stringId);
    }

    public void setBarTitle(CharSequence title, boolean isShowBack) {
        tv_font_title.setText(title);
        if(isShowBack){
            llt_font_left.setVisibility(View.VISIBLE);
            tv_font_title.setVisibility(View.VISIBLE);
        }else{
            llt_font_left.setVisibility(View.GONE);
            tv_font_title.setVisibility(View.GONE);
        }
    }


    //取出FrameLayout并调用父类removeAllViews()方法
    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        onContentChanged();
    }

    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //MobclickAgent.onResume(this);
        //JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //MobclickAgent.onPause(this);
        //JPushInterface.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //友盟统计-统计功能
        //MobclickAgent.setSessionContinueMillis(720000);
       // MobclickAgent.onKillProcess(this);
    }


}
